package com.changanw.askforleavedemo.service;

import com.changanw.askforleavedemo.mapper.UserMapper;
import com.changanw.askforleavedemo.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author changanw
 * @description
 * @date 2023/11/10 8:59
 */
@Service
public class UserService implements UserDetailsService {
  @Autowired
  UserMapper userMapper;
  @Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    User user = userMapper.loadUserByUsername(username);
    if (user == null){
      throw new UsernameNotFoundException("用户不存在");
    }
    user.setRoles(userMapper.getUserRolesByUserId(user.getId()));
    return user;
  }
}
